﻿using RimWorld;
using System;
using Verse;

namespace Devour_System
{
    //因为在XML的<comps>标签里，希望继承AbilityCompProperties 的类，故需要将CompAbilityEffect_Devour转换一下
    public class CompProperties_Devour : CompProperties_AbilityEffect
    {
        //构造函数，自动转换CompAbilityEffect_Devour
        public CompProperties_Devour()
        {
            this.compClass = typeof(CompAbilityEffect_Devour);
        }
    }

    //吞噬
    //吞噬技能对敌人使用后执行函数
    public class CompAbilityEffect_Devour : CompAbilityEffect
    {
        //技能使用后执行这个方法
        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest)
        {
            base.Apply(target, dest);
            //获取敌人对象
            Pawn pawn = target.Pawn;

            //判断对象必须是人类
            if(pawn == null || !pawn.RaceProps.Humanlike)
            {
                Messages.Message("目标必须是人类", this.parent.pawn, MessageTypeDefOf.NegativeEvent);
                return;
            }

            //执行吸收敌人技能和特性操作
            Transfer(pawn);

        }

        //吸收敌人技能
        private void Transfer(Pawn targetPawn)
        {
            //获取施法者对象
            Pawn pawn = this.parent.pawn;

            if (pawn == null || targetPawn == null)
            {
                Log.Error("施法者或目标是空的");
                return;
            }

            //获取敌人技能
            var skills = targetPawn.skills.skills;
            //获取敌人特性
            var traits = targetPawn.story.traits.allTraits;

            String message = "你解锁了：\n";

            if (pawn != null ) {
                //敌人的技能等级更高，就换为更高等级
                foreach(var skill in skills)
                {
                    //定义施法者啥技能增加多少经验值
                    SkillRecord skillRecord = pawn.skills.GetSkill(skill.def);
                    if (skill.Level > skillRecord.levelInt)
                    {
                        //如果目标的等级更高，则替换等级
                        skillRecord.levelInt = skill.Level;
                        skillRecord.xpSinceLastLevel = 0;
                        skillRecord.xpSinceMidnight = 0;
                        message += $"{skill.def.label}:{skill.Level}级\n";
                    }
                }
                message += "获得特性：\n";
                //获取敌人的特性并给施法者增加特性
                foreach (var trait in traits)
                {
                    //定义施法者获取特性
                    pawn.story.traits.GainTrait(trait);
                    message += $"{trait.Label}\n";
                }

                // 使用消息框通知玩家
                Messages.Message(message, pawn, MessageTypeDefOf.PositiveEvent);

                // 通过日志显示
                Log.Message(message);

            }
        }
    }
}
